/**
 * 作者:hanbo
 * 日期:2019/1/416:23
 * 描述:
 */

define(["dojo/_base/declare", "esri/widgets/FeatureForm"], function (declare, FeatureForm) {
    return declare("Edit", null, {
        constructor: function (view, layer) {
            this.view = view;
            this.layer = layer;
        },
        eidtAttributes: function () {
            var view = this.view;
            var featureLayer = this.layer;
            var that = this;
            // 关闭 popup
            view.popup.autoOpenEnabled = false;

            this.form = new FeatureForm({
                container: "form",
                layer: featureLayer,
                fieldConfig: [
                    {
                        name: "PEOPLE",
                        label: "权属人"
                    },
                    {
                        name: "DLMC",
                        label: "用地类型"
                    },
                    {
                        name: "QSDWMC",
                        label: "所属村组"
                    }
                ]
            });

            this.click = view.on("click", function (event) {
                // 清除上一次选择
                that.unselectFeature();
                // 监听点击事件
                view.hitTest(event).then(function (response) {
                    // If user selects a feature, select it
                    var results = response.results;
                    if (results.length > 0 && results[0].graphic && results[0].graphic.layer === featureLayer) {
                        that.selectFeature(results[0].graphic.attributes[featureLayer.objectIdField]);
                    }
                    else {
                        // 没有选中时，隐藏
                        document.getElementById("update").classList.add("esri-hidden");
                    }
                });
            });

            this.form.on("submit", function () {
                if (that.editFeature) {
                    // Grab updated attributes from the form.
                    var updated = that.form.getValues();

                    //更新全局信息
                    Object.keys(updated).forEach(function (name) {
                        that.editFeature.attributes[name] = updated[name];
                    });

                    // Setup the applyEdits parameter with updates.
                    var edits = {
                        updateFeatures: [that.editFeature]
                    };
                    that.applyAttributeUpdates(edits);
                }
            });

            document.getElementById("btnUpdate").onclick = function () {
                // Fires feature form's submit event.
                that.form.submit();
            };

            view.ui.add("update", "top-right");
        },
        //清除选择
        unselectFeature: function () {
            if (this.highlight) {
                this.highlight.remove();
            }
        },
        //选中
        selectFeature: function (objectId) {
            var view = this.view;
            var featureLayer = this.layer;
            var that = this;
            featureLayer.queryFeatures({
                objectIds: [objectId],
                outFields: ["*"],
                returnGeometry: true
            }).then(function (results) {
                if (results.features.length > 0) {
                    that.editFeature = results.features[0];

                    // 展示选中要素的信息
                    that.form.feature = that.editFeature;

                    // 高亮要素
                    view.whenLayerView(that.editFeature.layer).then(function (layerView) {
                        that.highlight = layerView.highlight(that.editFeature);
                    });

                    if (document.getElementById("update").classList.contains("esri-hidden")) {
                        document.getElementById("update").classList.remove("esri-hidden");
                    }
                }
            });
        },
        applyAttributeUpdates: function (params) {
            document.getElementById("btnUpdate").style.cursor = "progress";
            this.layer.applyEdits(params).then(function (editsResult) {
                if (editsResult.addFeatureResults.length > 0) {
                    var objectId = editsResult.addFeatureResults[0].objectId;
                    this.selectFeature(objectId);
                }
                document.getElementById("btnUpdate").style.cursor = "pointer";
            })
                .catch(function (error) {
                    console.log("===============================================");
                    console.error("[ applyEdits ] FAILURE: ", error.code, error.name,
                        error.message);
                    console.log("error = ", error);
                    document.getElementById("btnUpdate").style.cursor = "pointer";
                });
        },
        stopEdit: function () {
            this.unselectFeature();
            document.getElementById("update").classList.add("esri-hidden");
            this.click.remove();

            // 打开 popup
            this.view.popup.autoOpenEnabled = true;
        }
    })
});